
let request = require( 'request' );
const logger = require( "../utils/utils.logger" );
exports.getWordAndImg = ( req, res ) => {
    let options = {
        method: 'GET',
        headers: {
            "Access-Control-Allow-Origin": '*'
        }
    };
    
    var resObj = {
        "success": true,
        "data": {
            "month": "Jul",
            "day": "15",
            "zh": "真相是一种美丽又可怕的东西，需要格外谨慎地对待。",
            "en": "The truth is a beautiful and terrible thing, and should therefore be treated with great caution.",
            "pic": "https://staticedu-wps.cache.iciba.com/image/931eb280f7da7fa5031b58583dcb3d7d.jpg"
        }
    }
    var avatar = "https://ae01.alicdn.com/kf/HTB1ExdTbBCw3KVjSZFu763AOpXaX.png"
    let sorts = [ '男', '女', '动漫男', '动漫女' ]
    //return res.sendResultSimple(, 200, '获取成功' )
    request(
        'https://api.vvhan.com/api/dailyEnglish',
        options,
        function ( error, response, body ) {
            let responseBody = response.body ? JSON.parse( response.body ) : {}
            if ( error !== null ) {
                logger.error( `获取一段话错误错误：${req.url}` )
                return res.sendResultSimple( resObj, 200, '获取成功' )
            } 
            /*else { 
                resObj = Object.assign({},resObj,responseBody); 
            }*/
            
            let url = `https://api.uomg.com/api/rand.avatar?sort=${encodeURI(sorts[Math.ceil( Math.random() * 4 ) - 1])}&format=images`
            if(responseBody.success){
                resObj = Object.assign({},resObj,responseBody); 
                request(
                url,
                options,
                function ( error, response, body ) {
                    
                
                    if ( error !== null ) {
                        logger.error( `获取头像错误： ${req.url}` )
                        return res.sendResultSimple( resObj, 200, '获取成功' )
                    } else {
                        resObj.data.avatar = response.request ? response.request.href :avatar
                    }
                    
                    return res.sendResultSimple( resObj, 200, '获取成功' )
                } )
            }else{
                return res.sendResultSimple( resObj, 200, '获取成功' )
            }
           
        } )
};